package com.bwt.top.bwt_ad_flutter.manager;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bwt.top.bwt_ad_flutter.AdErrorUtil;
import com.bwt.top.bwt_ad_flutter.ad.BwtNativeExpressAd;
import com.bwt.top.bwt_ad_flutter.config.BwtAdEventConfig;
import com.bwt.top.bwt_ad_flutter.listener.OnAdReleaseListener;
import com.bwt.top.bwt_ad_flutter.model.AdParams;
import com.bwt.top.bwt_ad_flutter.util.LogUtil;
import com.bwt.top.bwt_ad_flutter.view.factory.BwtNativeExpressAdFactory;
import com.bwt.top.exception.AdError;

import java.util.HashMap;
import java.util.Map;

/**
 * 模板信息流广告管理器
 */
public class BwtNativeExpressAdManager implements OnAdReleaseListener {
    private static BwtNativeExpressAdManager instance;
    private final Map<String, BwtNativeExpressAd> bwtNativeExpressAdMap = new HashMap<>();

    private BwtNativeExpressAdManager() {
        BwtAdManager.getInstance().registerOnAdReleaseListener(this);
    }

    public static BwtNativeExpressAdManager getInstance() {
        if (instance == null) {
            synchronized (BwtNativeExpressAdManager.class) {
                if (instance == null) {
                    instance = new BwtNativeExpressAdManager();
                }
            }
        }
        return instance;
    }

    @Override
    public void onAdRelease(String adId) {
        release(adId);
    }

    /**
     * 检查并加载信息流模板广告
     *
     * @param adParams 广告参数
     */
    public void loadNativeExpressAd(Context context, @NonNull AdParams adParams) {
        LogUtil.log("信息流广告加载预备, adId: " + adParams.getAdId());
        if (!(context instanceof Activity) || ((Activity) context).isFinishing()) {
            LogUtil.log("信息流广告加载失败, adId: " + adParams.getAdId() + ", error: 当前上下文不适合获取信息流广告!");
            BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_FAILED, adParams, AdErrorUtil.getError(new AdError(-1, "当前上下文不适合获取信息流广告")));
            return;
        }

        BwtNativeExpressAd bwtNativeExpressAd = new BwtNativeExpressAd(adParams, (Activity) context);
        bwtNativeExpressAdMap.put(adParams.getAdId(), bwtNativeExpressAd);
        bwtNativeExpressAd.loadAd();
    }

    @Nullable
    public BwtNativeExpressAd getBwtNativeExpressAdByAdId(String adId) {
        return adId == null ? null : bwtNativeExpressAdMap.get(adId);
    }

    /**
     * 释放广告
     */
    private void release(String adId) {
        if (adId == null) return;
        try {
            BwtNativeExpressAd bwtNativeExpressAd = bwtNativeExpressAdMap.get(adId);
            if (bwtNativeExpressAd != null) {
                bwtNativeExpressAd.release();
                bwtNativeExpressAdMap.remove(adId);
            }

            BwtNativeExpressAdFactory factory = BwtAdManager.getInstance().getBwtNativeExpressAdFactory();
            if (factory != null) factory.release(adId);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
